<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="所属牧场:" name="orgId">
              <a-select
                show-search
                v-model:value="where.orgId"
                placeholder="请选择所属牧场"
                style="width: 205px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                allow-clear
              >
                <a-select-option v-for="item in ranchList" :key="item.ranchId" >
                  {{ item.ranchName }}
                </a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="oxtallId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          v-model:selection="selection"
          :scroll="{ x: 'max-content' }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space v-permission="['PROPERTY_OXTALL_ADD_BUTTON']">
              <a-button type="primary" @click="openEdit()">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>新建</span>
              </a-button>
            </a-space>
          </template>

          <template #bodyCell="{ column, record }">
            <!-- 排序字段可编辑 -->
            <template v-if="column.dataIndex === 'csort'">
              <div v-if="editingId === record.oxtallId">
                <a-space v-permission="['PROPERTY_OXTALL_UPDATE_SORT']">
                  <a-input 
                    v-model:value="editingValue" 
                    style="width: 90px"
                  />
                  <a-button type="link" size="small" @click="saveSort(record)">
                    <check-outlined />
                  </a-button>
                  <a-button type="link" size="small" @click="cancelEdit">
                    <close-outlined />
                  </a-button>
                </a-space>
              </div>
              <div v-else>
                <a-space>
                  <span>{{ record.csort }}</span>
                  <a-button v-permission="['PROPERTY_OXTALL_UPDATE_SORT']" type="link" size="small" @click="startEdit(record)">
                    <edit-outlined />
                  </a-button>
                </a-space>
              </div>
            </template>
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a v-permission="['PROPERTY_OXTALL_UPDATE_BUTTON']" @click="openEdit(record)">修改</a>
                <a-divider v-any-permission-and="[['PROPERTY_OXTALL_UPDATE_BUTTON'], ['PROPERTY_OXTALL_DEL_BUTTON', 'PROPERTY_OXTALL_FENCE_MGR', 'PROPERTY_OXTALL_AREA_MGR', 'PROPERTY_OXTALL_MGR']]" type="vertical" />
                <div v-permission="['PROPERTY_OXTALL_DEL_BUTTON']">
                  <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                    <a class="ele-text-danger">删除</a>
                  </a-popconfirm>
                </div>
                <a-divider v-any-permission-and="[['PROPERTY_OXTALL_UPDATE_BUTTON', 'PROPERTY_OXTALL_DEL_BUTTON'], ['PROPERTY_OXTALL_FENCE_MGR', 'PROPERTY_OXTALL_AREA_MGR', 'PROPERTY_OXTALL_MGR']]" type="vertical" />
                <a v-permission="['PROPERTY_OXTALL_FENCE_MGR']" @click="openFenceManageDialog(record)">牛栏</a>
                <a-divider v-any-permission-and="[['PROPERTY_OXTALL_UPDATE_BUTTON', 'PROPERTY_OXTALL_DEL_BUTTON', 'PROPERTY_OXTALL_FENCE_MGR'], [ 'PROPERTY_OXTALL_AREA_MGR', 'PROPERTY_OXTALL_MGR']]" type="vertical" />
                <a v-permission="['PROPERTY_OXTALL_AREA_MGR']" @click="openAreaManageDialog(record)">区域</a>
                <a-divider v-any-permission-and="[['PROPERTY_OXTALL_UPDATE_BUTTON', 'PROPERTY_OXTALL_DEL_BUTTON', 'PROPERTY_OXTALL_FENCE_MGR', 'PROPERTY_OXTALL_AREA_MGR'], ['PROPERTY_OXTALL_MGR']]" type="vertical" />
                <a v-permission="['PROPERTY_OXTALL_MGR']" @click="openManager(record)">牛舍管理</a>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>

    <!-- 编辑弹窗 -->
    <oxtall-edit v-if="showEdit" v-model:visible="showEdit" :data="current" @done="reload" />
    <oxtall-area v-if="showAreaManage" v-model:visible="showAreaManage" :data="current" @done="reload" />
    <oxtall-fence v-if="showFenceManage" v-model:visible="showFenceManage" :data="current" @done="reload" />
    <oxtall-manager-mgr-modal v-if="showMgr" v-model:visible="showMgr" :data="current" @done="reload" />
  </div>
</template>

<script>
import { PlusOutlined, EditOutlined, CheckOutlined, CloseOutlined } from '@ant-design/icons-vue';
import { OxtallApi } from '@/api/property/OxtallApi';
import OxtallEdit from './oxtall-edit.vue';
import OxtallArea from './oxtall-area.vue';
import { RanchApi } from '@/api/property/RanchApi';
import { message } from 'ant-design-vue';
import OxtallManagerMgrModal from './oxtallmanager/oxtallmgr-modal.vue'
import OxtallFence from './oxtall-fence.vue'

export default {
  name: 'Oxtall',
  components: {
    OxtallManagerMgrModal,
    PlusOutlined,
    EditOutlined,
    CheckOutlined,
    CloseOutlined,
    OxtallEdit,
    OxtallArea,
    OxtallFence
  },
  async mounted() {
    RanchApi.list({}).then(resp => {
      if (resp && resp.data &&  resp.data.length > 0) {
        this.ranchList = resp.data;
      }
    });
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '牛舍名字',
          dataIndex: 'oxtallName'
        },
        {
          title : '所属牧场',
          dataIndex: 'ranchName',
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '排序字段',
          dataIndex: 'csort'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示按钮管理弹窗
      showAreaManage: false,
      showMgr : false,
      showFenceManage : false,
      ranchList : [],
      editingId: null,
      editingValue: null,
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/03/02 22:49
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/03/02 22:49
     */
    reset() {
      this.where.orgId = '';
      this.reload();
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2022/03/02 22:49
     */
    async remove(row) {
      const result = await OxtallApi.delete({ oxtallId: row.oxtallId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2022/03/02 22:49
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    /**
     * 打开区域管理对话框
     *
     */
    openAreaManageDialog(row) {
      this.current = row;
      this.showAreaManage = true;
    },
    openManager(row) {
      this.current = row;
      this.showMgr = true;
    },
    openFenceManageDialog(row) {
      this.current = row;
      this.showFenceManage = true;
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return OxtallApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    },
    
    /**
     * 开始编辑排序字段
     */
    startEdit(record) {
      this.editingId = record.oxtallId;
      this.editingValue = record.csort;
    },

    /**
     * 取消编辑
     */
    cancelEdit() {
      this.editingId = null;
      this.editingValue = null;
    },

    /**
     * 保存排序值
     */
    async saveSort(record) {
      try {
        const updateData = {
          oxtallId: record.oxtallId,
          csort: this.editingValue
        };
        
        await OxtallApi.editCsort(updateData);
        message.success('排序更新成功');
        record.csort = this.editingValue;
        this.editingId = null;
        this.editingValue = null;
      } catch (error) {
        message.error('排序更新失败');
      }
    },
  }
};
</script>
